define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

	var Controller = {
		index: function () {
			// 初始化表格参数配置
			Table.api.init({
				extend: {
					index_url: 'qingdongwms/finance/consume/index',
					detail_url: 'qingdongwms/customer/consume/detail',
					edit_consume: 'qingdongwms/customer/consume/edit',
					add_url: 'qingdongwms/customer/consume/add',
					table: 'finance',
				}
			});

			var table = $("#table");
			$(".btn-add").data("area", ["90%", "90%"]);
			$(".btn-edit").data("area", ["90%", "90%"]);
			// 初始化表格
			table.bootstrapTable({
				url: $.fn.bootstrapTable.defaults.extend.index_url + '?createtime=' + Fast.api.query("createtime"),
				pk: 'id',
				sortName: 'id',
				fixedColumns: true,
				fixedNumber: 2,
				search: false,
				searchFormVisible: true,
				columns: [
					[
						// { field: 'state', checkbox: true },
						{ field: 'staff_id', title: __('报销员工'), searchList: $.getJSON("qingdongwms/finance/staff_wage/getstaff"), visible: false },
						{
							field: 'number', title: __('报销单号'), operate: false, formatter: function (value, row, index) {

								return "<a href='javascript:void(0);' class='btn-dialog'  data-shade=\"0.3\"\n" +
									"data-url=\"" + $.fn.bootstrapTable.defaults.extend.detail_url + "?ids=" + row.id +
									"\" data-title=\"费用详情\" ' >" + value + "</a>";
							}
						},
						{ field: 'submit_date', title: __('消费日期'), operate: '=', addclass: 'datetimepicker', data: "data-date-format=\" YYYY-MM-DD\"" },

						// {
						// 	field: 'customer', title: __('关联客户'), operate: false, formatter: function (value, row, index) {
						// 		if (row.customer) {
						// 			return "<a href='javascript:void(0);' data-id='" + row.customer.id + "' class='show-customer'>" + row.customer.name + "</a>";
						// 		} else {
						// 			return '';
						// 		}
						// 	}
						// },
						// { field: 'customer_id', title: '关联客户', visible: false, addClass: "selectpage", extend: "data-source='qingdongwms/customer/customer/index' data-field='name'" },

						{ field: 'money', title: __('消费金额') },
						{ field: 'check_status', title: __('审批状态'), formatter: Table.api.formatter.status, searchList: { 0: __('待审核'), 1: __('审核中'), 2: __('审核通过'), 3: __('审核拒绝'), 4: __('撤销'), 5: __('草稿') }, custom: { 0: 'gray', 1: 'gray', 2: 'success', 3: 'danger', 4: 'danger', 5: 'danger' } },
						{ field: 'remark', title: __('备注') },
						{ field: 'staff.name', title: __('报销员工'), operate: false },
						{
							field: 'createtime',
							title: __('Createtime'),
							formatter: Table.api.formatter.datetime,
							operate: 'RANGE',
							addclass: 'datetimerange',
							sortable: true,
							width: 150
						},
						{
							field: 'operate', title: __('Operate'),
							buttons: [{
								name: 'consume_set',
								text: __('修改'),
								title: __('修改'),
								icon: 'fa fa-add',
								classname: 'btn btn-xs btn-warning btn-dialog',
								url: $.fn.bootstrapTable.defaults.extend.edit_consume,
								extend: 'data-area=\'["100%","100%"]\'',
								visible: function (row) {
									if (row.check_status == 3) {
										return true;
									} else {
										return false;
									}
								},
							}],
							table: table, events: Table.api.events.operate,
							formatter: Table.api.formatter.operate
						}

					]
				]
			});
			$(document).on('click', '.show-detail', function (data) {
				var area = ['95%', '95%'];
				var options = {
					shadeClose: false,
					shade: [0.3, '#393D49'],
					area: area,
					callback: function (value) {
						//在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
						console.log(value);
					}
				};
				Fast.api.open($.fn.bootstrapTable.defaults.extend.detail_url + "?ids=" + $(this).data('id'), '费用详情页', options);
			}).on('click', '.show-customer', function (data) {
				var area = ['95%', '95%'];
				var options = {
					shadeClose: false,
					shade: [0.3, '#393D49'],
					area: area,
					callback: function (value) {
						//在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
						console.log(value);
					}
				};
				Fast.api.open("qingdongwms/customer/customer/detail?ids=" + $(this).data('id'), '客户详情', options);
			});
			// 为表格绑定事件
			Table.api.bindevent(table);
		},

		api: {
			bindevent: function () {
				Form.api.bindevent($("form[role=form]"), function (data, ret) {
					//这里是表单提交处理成功后的回调函数，接收来自php的返回数据
					Fast.api.close(data);//这里是重点
				});
			}
		}
	};
	return Controller;
});